package com.android.lovingpet.common;

import android.os.Bundle;
import android.util.Log;

import com.android.lovingpet.bean.JsonIds;
import com.android.lovingpet.util.CallbackStore;
import com.android.lovingpet.util.HttpUtil;
import com.android.lovingpet.util.MyHttp;
import com.android.lovingpet.util.URLStore;
import com.android.lovingpet.util.Utility;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FileModel {

    private static FileModel instance;
    private FileModel() {}
    public static FileModel getInstance() {
        if (instance == null) {
            synchronized (FileModel.class) {
                instance = new FileModel();
            }
        }
        return instance;
    }

    /***
     * 上传头像
     * @param type 0：team 1：person 2：pet 3：host
     */
    public void uploadImage(int id, int type, File image, Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("type", type + "");
        Map<String, File> files = new HashMap<>();
        files.put("image", image);
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.IMAGE_UPLOAD, params, files, new CallbackStore.CallbackNon<>(back));
    }

    public void downloadImage(int id, int type, FileBack back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("type", type + "");
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.IMAGE_DOWNLOAD, params, new CallbackStore.CallbackImpl<FileBack>(back){
            @Override
            public void onResponse(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    back.loadSuccess(object.getString("image"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 不支持多图片上传
     * @param mode text 0, photo 1, video 2
     * @param id 用户id
     * @param userType team 0, person 1, pet 2, host 3, post 4
     * @param fileType text: 只有组织可以传, 基地工作守则 0, 动物领养细则 1, 组织负责人介绍 2（生成的文件为.doc后缀）
     *                  photo: 身份证正面 0, 身份证反面 1, 组织证明 2， 寄养家庭中的宠物头像 3
     *                  video: 待领养宠物的视频 0
     * @param file 要上传的单文件
     */
    public void uploadFile(int mode, int id, int userType, int fileType, File file, Back back) {
        String fileTypeKey = "";
        String fileKey = "";
        String url = "";
        switch (mode) {
            case 0:fileTypeKey = "textType";fileKey = "text";url = URLStore.TEXT_UPLOAD;break;
            case 1:fileTypeKey = "photoType";fileKey = "photo";url = URLStore.PHOTO_UPLOAD;break;
            case 2:fileTypeKey = "videoType";fileKey = "video";url = URLStore.VIDEO_UPLOAD;break;
        }
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("userType", userType + "");
        params.put(fileTypeKey, fileType + "");
        Map<String, File> files = new HashMap<>();
        files.put(fileKey, file);
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), url, params, files, new CallbackStore.CallbackNon<>(back));
    }

    /**
     *
     * param 所有参数对应上面的方法
     */
    public void downloadFile(int mode, int id, int fileType, FileBack back) {
        String fileTypeKey = "";
        String url = "";
        switch (mode) {
            case 0:fileTypeKey = "textType";url = URLStore.TEXT_DOWNLOAD;break;
            case 1:fileTypeKey = "photoType";url = URLStore.PHOTO_DOWNLOAD;break;
            case 2:fileTypeKey = "videoType";url = URLStore.VIDEO_DOWNLOAD;break;
        }
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put(fileTypeKey, fileType + "");
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), url, params, new CallbackStore.CallbackImpl<FileBack>(back) {
            @Override
            public void onResponse(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    back.loadSuccess(object.getString("url"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     *
     * @param userType 0：team___ 1：person___ 2：pet___ 3：host___ 4：post___
     * @param albumType 0：动态 1：相册
     */
    public void downloadAlbum(int id, int userType, int albumType, AlbumBack back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("userType", userType + "");
        params.put("albumType", albumType + "");
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.ALBUM_DOWNLOAD, params, new CallbackStore.CallbackImpl<AlbumBack>(back) {
            @Override
            public void onResponse(String json) throws JSONException {
                JsonIds jsonIds = new Gson().fromJson(json, JsonIds.class);
                List<String> album = jsonIds.getUrl();
                if (album == null) {
                    album = new ArrayList<>();
                }
                back.loadSuccess(album);
            }
        });
    }

    public void uploadAlbum(int id, int userType, int albumType, List<File> album, Back back) {
        HashMap<String, String> params = new HashMap<>();
        params.put("id", id + "");
        params.put("userType", userType + "");
        params.put("albumType", albumType + "");
        String[] keys = new String[album.size()];
        File[] files = new File[album.size()];
        Arrays.fill(keys, "album");
        MyHttp.getInstance().postAsync(Utility.getJSessionId(),URLStore.ALBUM_UPLOAD, params, keys, album.toArray(files), new CallbackStore.CallbackNon<>(back));
    }

    public interface FileBack extends Back {
        void loadSuccess(String url);
    }

    public interface AlbumBack extends Back {
        void loadSuccess(List<String> images);
    }

    public void uploadVideo(HashMap params, Back back)
    {
        /*HttpUtil.postAsync( Utility.getJSessionId(),URLStore.VIDEO_UPLOAD, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                Log.d("uploadVideo",responseData);
                int code= HttpUtil.getCode(responseData);
                Bundle bundle=new Bundle();
                bundle.putInt("code",code);
                back.onSuccess(bundle);
            }
        });*/

    }
}
